//
//  QShowPushView.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/7/25.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QShowPushView.h"
#import "QPayPushView.h"
@implementation QShowPushView{
    UIControl * _maskView;
    QPayPushView * _subView;
    CGFloat W;
}

- (instancetype)initWithModel:(QPushModel *)model{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = [UIScreen mainScreen].bounds;
        W = 280;
        [self configViewWithTitle:model];
    }
    return self;
}
- (void)configViewWithTitle:(QPushModel *)model{
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    _maskView.hidden = YES;
    [self addSubview:_maskView];
    
    _subView = [[[NSBundle mainBundle] loadNibNamed:@"QPayPushView" owner:self options:nil] lastObject];
    _subView.frame = CGRectMake((kScreenWidth - W)/2, kScreenHeight, W, 200);
    __block QShowPushView *weakSelf = self;
    _subView.CloseBlock = ^(NSInteger index) {
        [weakSelf hidePlayBoxView1];
        if (weakSelf.block) {
            weakSelf.block(index);
        }

    };
    
    
    [_maskView addSubview:_subView];
}

- (void)setModel:(QPushModel *)model{
    _model = model;
    _subView.model = _model;
}

- (void)showPlayBoxView{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    
    [window addSubview:_maskView];
    _maskView.hidden = NO;
    _maskView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _subView.center = self.center;
        _maskView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidePlayBoxView1{
    
    _maskView.alpha = 1;
    [UIView animateWithDuration:.3 animations:^{
        _maskView.alpha = 0;
    } completion:^(BOOL finished) {
        _maskView.hidden = YES;
        [_maskView removeFromSuperview];
        [_subView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
